package com.service.admin.member.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.admin.dto.LoginUserSession;
import com.service.admin.member.controller.rep.FeedbackDetailRep;
import com.service.admin.member.controller.rep.FeedbackPageRep;
import com.service.admin.member.controller.req.FeedbackPageReq;
import com.service.admin.member.controller.req.FeedbackReplyReq;
import com.service.core.exception.CommonException;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.NotifyMessageEnums;
import com.service.dao.mapper.NotifyMessageMapper;
import com.service.dao.model.NotifyMessage;
import com.service.dao.query.FeedbackQueryParams;
import com.service.dao.enums.FeedbackEnum;
import com.service.dao.mapper.FeedbackMapper;
import com.service.dao.model.Feedback;
import com.service.admin.member.service.FeedbackService;
import com.service.admin.support.SessionUtil;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("feedbackService")
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private NotifyMessageMapper notifyMessageMapper;

    @Override
    public CommonPageRep<FeedbackPageRep> pageFeedback(FeedbackPageReq pageReq) {
        Page<Feedback> page = PageHelper.startPage(pageReq.getPageNo(), pageReq.getPageSize());
        FeedbackQueryParams params = ModelMapperUtil.getStrictModelMapper().map(pageReq, FeedbackQueryParams.class);
        baseMapper.page4MemberByAdmin(params);
        List<FeedbackPageRep> pageList = page.getResult().stream().map(item -> ModelMapperUtil
                .getStrictModelMapper().map(item, FeedbackPageRep.class)).collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, pageList);
    }

    @Override
    public FeedbackDetailRep getDetailById(Integer id) {
        Feedback feedback = baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(feedback)) {
            throw new CommonException("反馈不存在");
        }
        return ModelMapperUtil.getStrictModelMapper().map(feedback, FeedbackDetailRep.class);
    }

    @Override
    public void replyFeedback(FeedbackReplyReq replyReq) {
        Feedback feedback = baseMapper.selectById(replyReq.getFeedbackId());
        if (ObjectUtils.isEmpty(feedback)) {
            throw new CommonException("反馈不存在");
        }

        feedback = new Feedback();
        feedback.setId(replyReq.getFeedbackId());

        LoginUserSession userSession = sessionUtil.getUser();
        feedback.setReplyUserId(userSession.getId());
        feedback.setReplyUserName(userSession.getNickName());
        feedback.setReplyContent(replyReq.getContent());
        feedback.setReplyTime(new Date());
        feedback.setFeedbackStatus(FeedbackEnum.Status.reply.getCode());
        baseMapper.updateById(feedback);

    }
}
